import { type Observable, Subject, iif, of } from "rxjs";

const streamFirst$ = of("first");
const streamSecond$ = of("second");

const subscribeToFirst = new Subject<boolean>();

let subscribeToFirstEmitter: boolean;
let firstOrSecond: Observable<string>;

subscribeToFirst.subscribe((val) => {
  subscribeToFirstEmitter = val;

  initFirstOrSecond();
});

function initFirstOrSecond() {
  firstOrSecond = iif(
    () => subscribeToFirstEmitter,
    streamFirst$,
    streamSecond$,
  );
  firstOrSecond.subscribe((value) => console.log(value));
}

console.log("firstOrSecond");

initFirstOrSecond();

setTimeout(() => {
  subscribeToFirst.next(true);
  console.log("fin");
}, 1000);
